package com.ytktt.study.week8.task6.hmily.tcc.order.service.impl;

import com.ytktt.study.week8.task6.hmily.tcc.order.entity.OrderEntity;
import com.ytktt.study.week8.task6.hmily.tcc.order.mapper.OrderMapper;
import com.ytktt.study.week8.task6.hmily.tcc.order.service.OrderService;
import com.ytktt.study.week8.task6.hmily.tcc.order.service.PaymentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hmily.common.utils.IdWorkerUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * TODO
 *
 * @author ytk
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderServiceImpl implements OrderService {

    private final OrderMapper orderMapper;
    private final PaymentService paymentService;

    @Override
    public String orderPay(Integer itemAmount, BigDecimal payAmount) {
        OrderEntity entity = this.saveOrder(itemAmount, payAmount);
        long start = System.currentTimeMillis();
        this.paymentService.makePayment(entity);
        System.out.println("切面耗时：" + (System.currentTimeMillis() - start));
        return "success";
    }

    @Override
    public String testOrderPay(Integer itemAmount, BigDecimal payAmount) {
        OrderEntity entity = this.saveOrder(itemAmount, payAmount);
        final long start = System.currentTimeMillis();
        this.paymentService.testMakePayment(entity);
        System.out.println("方法耗时：" + (System.currentTimeMillis() - start));
        return "success";
    }

    @Override
    public String orderPayWithNested(Integer itemAmount, BigDecimal payAmount) {
        OrderEntity entity = this.saveOrder(itemAmount, payAmount);
        this.paymentService.makePaymentWithNested(entity);
        return "success";
    }

    @Override
    public String orderPayWithNestedException(Integer itemAmount, BigDecimal payAmount) {
        OrderEntity entity = this.saveOrder(itemAmount, payAmount);
        this.paymentService.makePaymentWithNestedException(entity);
        return "success";
    }

    @Override
    public String mockItemStockWithTryException(Integer itemAmount, BigDecimal payAmount) {
        OrderEntity entity = this.saveOrder(itemAmount, payAmount);
        return this.paymentService.mockPaymentItemStockWithTryException(entity);
    }

    @Override
    public String mockItemStockWithTryTimeout(Integer itemAmount, BigDecimal payAmount) {
        OrderEntity entity = this.saveOrder(itemAmount, payAmount);
        this.paymentService.mockPaymentItemStockWithTryTimeout(entity);
        return "success";
    }

    @Override
    public String mockAccountWithTryException(Integer itemAmount, BigDecimal payAmount) {
        OrderEntity entity = this.saveOrder(itemAmount, payAmount);
        this.paymentService.mockPaymentAccountWithTryException(entity);
        return "success";
    }

    @Override
    public String mockAccountWithTryTimeout(Integer itemAmount, BigDecimal payAmount) {
        OrderEntity entity = this.saveOrder(itemAmount, payAmount);
        this.paymentService.mockPaymentAccountWithTryTimeout(entity);
        return "success";
    }

    @Override
    public String mockItemStockWithConfirmTimeout(Integer itemAmount, BigDecimal payAmount) {
        OrderEntity entity = this.saveOrder(itemAmount, payAmount);
        this.paymentService.mockPaymentItemStockWithConfirmTimeout(entity);
        return "success";
    }

    private OrderEntity saveOrder(Integer count, BigDecimal amount) {
        OrderEntity order = new OrderEntity();
        order.setUserId(1);
        order.setItemId(1);
        order.setItemPrice(new BigDecimal(10));
        order.setAmount(count);
        order.setOrderPrice(amount);
        order.setStatus((byte) 0);
        long now = System.currentTimeMillis();
        order.setCreateTime(now);
        order.setUpdateTime(now);
        this.orderMapper.insertSelective(order);
        return order;
    }
}
